package cn.gdpu.course.web.controller.student;

import cn.gdpu.course.constant.Constant;
import cn.gdpu.course.dao.CourseDao;
import cn.gdpu.course.dao.CourseStudentDao;
import cn.gdpu.course.dao.StudentDao;
import cn.gdpu.course.dao.domain.Course;
import cn.gdpu.course.dao.domain.CourseStudent;
import cn.gdpu.course.dao.domain.Student;
import cn.gdpu.course.web.bean.PageBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/2.
 */

@Controller
@RequestMapping("/student/return")
public class StuReturnController {

	@Resource
	private CourseStudentDao courseStudentDao;

	@Resource
	private StudentDao studentDao;

	@Resource
	private CourseDao courseDao;


	@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
	public String index(HttpServletRequest request, ModelMap model) {
		String username = (String) request.getSession().getAttribute(Constant.user_name);
		Student student = new Student();
		student.setSname(username);
		student = studentDao.findBySname(student);
		model.addAttribute("username", username);
		model.addAttribute("studentno", student.getSno());
		return "student/return";
	}

	@ResponseBody
	@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
	public PageBean<CourseStudent> list(ModelMap model, CourseStudent courseStudent) {
		PageBean<CourseStudent> data = new PageBean<CourseStudent>();

		if (null == courseStudent.getSno() || "".equals(courseStudent.getSno())){
			data.setData(new ArrayList<CourseStudent>());
			return data;
		}

		List<CourseStudent> courseStudents = courseStudentDao.find(courseStudent);
		data.setData(courseStudents);
		return data;
	}

	@ResponseBody
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public int delete(ModelMap model, CourseStudent courseStudent) {
		Student student = new Student();
		student.setSno(courseStudent.getSno());
		student = studentDao.findBySno(student);
		if (student == null) {
			return -1;
		}

		Course course = new Course();
		course.setBno(courseStudent.getBno());
		course = courseDao.findByBno(course);
		if (course == null) {
			return -2;
		}

		/**
		 * 并未选择此课程
		 * 则返回-3
		 */
		CourseStudent bs1 = new CourseStudent();
		bs1.setSid(student.getId());
		bs1.setBid(course.getId());
		List<CourseStudent> bss1 = courseStudentDao.find(bs1);
		if (CollectionUtils.isEmpty(bss1)) {
			return -3;
		}

		courseStudent.setSid(student.getId());
		courseStudent.setBid(course.getId());
		boolean rs1 = courseStudentDao.delete(courseStudent);

		Course b = new Course();
		b.setId(course.getId());
		b.setRemain(course.getRemain() + 1);
		boolean rs2 = courseDao.updateRemain(b);


		return rs1 && rs2 ? 1 : 0;
	}

	/**
	 * 以下废弃
	 * @param student
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/findStudent", method = {RequestMethod.GET, RequestMethod.POST})
	public Student findStudent(Student student) {
		List<Student> students = studentDao.find(student);
		if (CollectionUtils.isEmpty(students)) {
			return null;
		} else {
			return students.get(0);
		}
	}

	/**\
	 * 以下废弃
	 * @param course
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/findCourse", method = {RequestMethod.GET, RequestMethod.POST})
	public Course findCourse(Course course) {
		return courseDao.findByBno(course);
	}

}
